import unittest
import requests


# 使用unittest框架进行测试

class TestLogin(unittest.TestCase):
    def setUp(self) -> None:
        print("-----setUp-----")

    def tearDown(self) -> None:
        print("-----tearDown-----")

    @classmethod
    def setUpClass(cls) -> None:
        print("=====setUpClass======")

    @classmethod
    def tearDownClass(cls) -> None:
        print("=====tearDownClass======")

    def testlogin_ok(self):
        # 登录（get）
        resp1 = requests.get(url="http://localhost:8080/springbooth42y92e1/users/login?username=admin&password=admin")

        # 登录(post)
        #resp2 = requests.post(url="http://localhost:8080/springbooth42y92e1/users/login",
                             # data={"username": "admin", "password": "admin"})
        # 打印登录结果
        print(resp1.json())

        #获取令牌
        my_token = resp1.json().get("token")

        #使用登录成功令牌进行员工添加
        resp3 = requests.post(url="http://localhost:8080/springbooth42y92e1/yonghu/save",
                              headers={"Token": my_token},
                              json={"yonghuzhanghao": "1111", "mima": "111", "yonghuxingming": "1", "touxiang": "upload/1746851925554.jpg"
                                  , "xingbie": "男", "nianling": 17, "lianxifangshi": "16696689480"})
        #打印添加结果
        print(resp3.json())
        yonghu_data= resp3.json().get("data")

        # # 用户修改
        # resp4 = requests.post(url="http://localhost:8080/springbooth42y92e1/yonghu/update",
        #                       headers={"Token": my_token},
        #                       json={"id": yonghu_data,"yonghuzhanghao": "1118", "mima": "111", "yonghuxingming": "1", "touxiang": ""
        #                           , "xingbie": "男", "nianling": 18, "lianxifangshi": "16696689480"},
        #                       )
        # print(resp4.json())

        # 单个用户查询
        # resp5 = requests.get(url=f"http://localhost:8080/springbooth42y92e1/yonghu/info/{yonghu_data}",
        #                      headers={"Token": my_token})
        # print(resp5.json())

        # 用户删除
        resp6 = requests.post(url="http://localhost:8080/springbooth42y92e1/yonghu/update",
                              headers={"Token": my_token},
                              json={yonghu_data},
                              )
        print(resp6.json())
        # 所有用户查询
        resp7 = requests.get(url="http://localhost:8080/springbooth42y92e1/yonghu/page?page=1&limit=10&sort=id&order=desc",
                             headers={"Token": my_token},)
        print(resp7.json())




        # 断言
        self.assertEqual(200, resp3.status_code)

        self.assertEqual(0, resp3.json().get("code"))

    def test02_ok(self):
        resp = requests.get(url="http://cx.shouji.360.cn/phonearea.php?number=16696689480")

        print(resp.json())
        # 断言
        self.assertEqual(200, resp.status_code)

        self.assertEqual(0, resp.json().get("code"))

        self.assertEqual("河南", resp.json().get("data").get("province"))
